package autotd.server;

import java.net.*;

import java.util.*;
import java.awt.Point;
import java.io.*;

import autotd.*;

public class AdvancedSimulatorListenerClient implements NetworkProtocol {
	private BufferedReader myInput;
	private List<AdvancedSimulatorListener> myListeners;

	public AdvancedSimulatorListenerClient() throws Exception {
		myListeners = new Vector<AdvancedSimulatorListener>();
		Socket connector = new Socket("localhost", DEFAULT_PORT_NUMBER);
		PrintStream output = new PrintStream(connector.getOutputStream());
		myInput = new BufferedReader(new InputStreamReader(connector.getInputStream()));
		waitForMessage(myInput, CONNECTED_RESPONSE);
		output.println(WATCHER_REQUEST);
		output.flush();
		waitForMessage(myInput, ACK);
		System.out.println("AdvancedSimulatorListenerClient connected.");
		(new Thread() {
			public void run() {
				processAllInput();
			}
		}).start();
	}

	public void addAdvancedListener(AdvancedSimulatorListener al) {
		myListeners.add(al);
	}
	
	public static void waitForMessage(BufferedReader input, String msg) {
		do {
			try {
				String line = input.readLine();
				if (line.equals(msg)) {
					return;
				}
			} catch(Exception exp) {
				System.err.println("Exception occurred: " + exp);
			}
		} while (true);
	}

	public static boolean getResponse(BufferedReader input) {
		do {
			try {
				String line = input.readLine();
				if (line.equals(ACK)) {
					return true;
				} else if (line.equals(FAIL)) {
					return false;
				} else if (line.length() > 0) {
					System.err.println("Unknown response: " + line);
				}
			} catch(Exception exp) {
				System.err.println("Exception occurred: " + exp);
				return false;
			}
		} while (true);
	}


	public void processAllInput() {
		try {
		String nextLine = myInput.readLine();
		while (nextLine != null) {
			try {
				if (nextLine.length() == 0) {
					nextLine = myInput.readLine();
					continue;
				}

				String[] tokens = nextLine.split(" ");

				if (tokens[0].equals(MONSTER_UPDATE)) {
					if (tokens[1].equals(MONSTER_CREADTED_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.monsterCreated(
								Integer.parseInt(tokens[2]), tokens[3],
								Integer.parseInt(tokens[4]),
								Integer.parseInt(tokens[5]), Integer.parseInt(tokens[6])
							);
						}
					} else if (tokens[1].equals(MONSTER_MOVED_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.monsterMoved(
								Integer.parseInt(tokens[2]),
								Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4]),
								Integer.parseInt(tokens[5]), Integer.parseInt(tokens[6])
							);
						}
					} else if (tokens[1].equals(MONSTER_INJURED_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.monsterInjured(
								Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3])
							);
						}
					} else if (tokens[1].equals(MONSTER_COMPLETED_PATH_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.monsterCompletedPath(
								Integer.parseInt(tokens[2])
							);
						}
					} else {
						System.err.println("Unknown monster update: " + nextLine);
					}
				} else if (tokens[0].equals(TOWER_UPDATE)) {
					if (tokens[1].equals(TOWER_PLACED_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.towerPlaced(
								Integer.parseInt(tokens[2]), tokens[3],
								Integer.parseInt(tokens[4]), Integer.parseInt(tokens[5]),
								Integer.parseInt(tokens[6])
							);
						}
					} else if (tokens[1].equals(TOWER_FIRED_UPDATE)) {
						for (AdvancedSimulatorListener asl : myListeners) {
							asl.towerFired(
								Integer.parseInt(tokens[2]), Integer.parseInt(tokens[3])
							);
						}
					} else {
						System.err.println("Unknown tower update: " + nextLine);
					}
				} else if (tokens[0].equals(PLAYER_UPDATE)) {
					for (AdvancedSimulatorListener asl : myListeners) {
						asl.playerChange(Integer.parseInt(tokens[1]), Integer.parseInt(tokens[2]));
					}
				} else if (tokens[0].equals(TURN_UPDATE)) {
					for (AdvancedSimulatorListener asl : myListeners) {
						asl.turnChangedTo(Integer.parseInt(tokens[1]));
					}
				} else {
					System.err.println("Unknown update type: " + nextLine);
				}
			} catch(Exception e) {
				System.err.println("Cmd " + nextLine + " failed because of " + e);
			}
			nextLine = myInput.readLine();
		}
	  } catch(Exception e) {
		System.err.println("AdvancedSimulatorClient failed because of " + e);
	  }
	System.out.println("AdvancedSimulatorListenerClient connected.");
	}
}





